/**
 * @(#) MedicalMoneyProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	건강보험증 Proxy - Proxy
 * 				건강보험증를 조회를한다.
 */
package com.skt.common_hr.babybt.model
{
	import com.skt.common_hr.babybt.model.vo.MemberVO;
	import com.skt.common_hr.babybt.model.vo.mainList2VO;
	import com.skt.common_hr.babybt.model.vo.mainListVO;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class Babybt_imsanbuProxy extends CommonProxy
	{
		public static const NAME:String = "Babybt_imsanbuProxy";

		public static const LOAD_MEDICALMONEY_LIST_COMPLETE:String	= "LoadMedicalMoneyListComplete";
		public static const LOAD_MEDICALMONEY_LIST_FAILED:String	= "LoadMedicalMoneyListFailed";


		public var mainList:ArrayCollection;
		public var acGbn:ArrayCollection;

		public var key:String;

		//지원기준
		public var loadFaultMessage:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function Babybt_imsanbuProxy(data:Object=null)
		{
			super(NAME, data);
		}

		// 메인리스트 호출
		public function loadList( setFunc:Function, begda:Date, endda:Date):void
		{
			var obj:Object = {};
			obj.I_PERNR	= Variables.myPernr;
			obj.I_BEGDA	= DateUtil.toBasicString(begda);
			obj.I_ENDDA	= DateUtil.toBasicString(endda);
			loadData("Z_HR_BV01", obj, loadListResult, listFault, setFunc);
		}
		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				mainList = parseMainList(event.result.OTAB);
				event.result.func(mainList);
			}
			else {
				//Alert.show(retText);
				mainList = new ArrayCollection();
				event.result.func(mainList);
			}
		}
		
		public var sListWriteChkMsg:String='';
		public var dBegda:Date;
		public var dEndda:Date;
		public var dFbegda:Date;
		public var dFEndda:Date;
		


		// 삭제
 		public function del(_GWAREKEY:String, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_GWAREKEY"] = _GWAREKEY;
			//params["I_PERNR"] = Variables.myPernr;
			loadData("Z_HR_BV02", params, delResult, listFault, setFunc);
		}
		public function delResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}
			else {
				Alert.show('delResult err: ' + retText);
			}
		}


		// 저장
		public var _dv:String;
		public function sendSave(obj:Object, setFunc:Function, dv:String, gwarekey:String=''):void
		{
			// 임시저장인지 결재까지인지 체크..변수 1:TEMP, 2:APPROVAL
			_dv = dv;
			
			var tables:Object = new Object();
			var table:Array = new Array();
			var params:Object = new Object();
			
			params["I_GWAREKEY"] 	= gwarekey;
			table.push(obj);
			tables["ITAB"] = table;
			
			saveData("Z_HR_BV03", params, tables, saveOrderListResult, listFault, setFunc);
		}
		

		public function saveOrderListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				//event.result.func(_dv, event.O_GWAREKEY.toString() );
				var gwKey:String = event.result.O_GWAREKEY;
				var orgtx:String = event.result.O_ORGTX;
				var ename:String = event.result.O_ENAME;
				
				event.result.func(_dv, gwKey, orgtx, ename);
			}			
			else {
				Alert.show(retText);
			}
		}



		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				var retObj:mainListVO = new mainListVO();			
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
                          continue;
                     }
                         
					if(field == "SDATE" || field == "BEGDA" || field == "ENDDA" ) {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);						
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);
					}
					else if(field == "PERNR" ) {
						retObj[field] = parseInt(obj[field]);
					}
									
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}


		private function parseMainList2(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				var retObj:mainList2VO = new mainList2VO();			
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
                          continue;
                     }
                         
					if(field == "SDATE" || field == "BEGDA" || field == "ENDDA" ) {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);						
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);
					}			
					else if(field == "PERNR" ) {
						retObj[field] = parseInt(obj[field]);
					}						
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}

		private function parseGbnCode(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();

			for each(var obj:Object in recArr){
				var retObj:Object = new Object();			
				retObj.TEXT = obj.TEXT;
				retObj.CODE = obj.CODE;
				retArr.addItem(retObj);
			}
			return retArr;
		}

		private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);
				}
				else {
					retObj[field] = vo[field];
				}
				trace(retObj[field]);
			}
			return retObj;
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}


	}
}